
struct Town {
    let region: String

    var population: Int {
        didSet(oldPopulation) {
            print("The population has changed to \(population) from \(oldPopulation).")
        }
    }

    var numberOfStoplights: Int

    init(region: String, population: Int, stoplights: Int) {
        self.region = region
        self.population = population
        numberOfStoplights = stoplights
    }

    enum Size {
        case small
        case medium
        case large
    }
}

class Vehicle {
    var numberOfWheels: Int
    var color: String

    // 指定初始化器
    init(numberOfWheels: Int, color: String) {
        self.numberOfWheels = numberOfWheels
        self.color = color
    }
}

// 子类重写指定初始化器
class Bicycle: Vehicle {
    var hasBasket: Bool

    // 指定初始化器
    override init(numberOfWheels: Int, color: String) {
        self.hasBasket = false // 子类特有的属性初始化
        super.init(numberOfWheels: numberOfWheels, color: color) // 调用父类的指定初始化器
    }

    // 另一个指定初始化器
    convenience init(color: String) {
        self.init(numberOfWheels: 2, color: color)
        self.hasBasket = true
    }
}

// 使用指定初始化器创建实例
let myBicycle = Bicycle(numberOfWheels: 2, color: "Blue")
print("My bicycle has \(myBicycle.numberOfWheels) wheels and is \(myBicycle.color).")

// 使用便捷初始化器创建实例
let anotherBicycle = Bicycle(color: "Red")
print("Another bicycle has \(anotherBicycle.numberOfWheels) wheels and is \(anotherBicycle.color).")

